import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/5/11
 */
public class Demo31 {
    static int N = 110;
    static int[][] g = new int[N][N];
    static int[][] d = new int[N][N];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                g[i][j] = scanner.nextInt();

        System.out.println(bfs(n, m));
        scanner.close();
    }
    public static int bfs(int n,int m) {
        Queue<int[]> queue = new LinkedList<>();
        d[0][0] = 0;
        int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
        queue.offer(new int[]{0,0});

        while(!queue.isEmpty()){
            int a[] = queue.poll();
            for(int i = 0 ; i < 4; i++){
                int x = a[0] + dx[i],y = a[1] + dy[i];

                if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == 0){
                    d[x][y] = d[a[0]][a[1]] + 1;
                    queue.offer(new int[]{x,y});
                }
            }
        }
        return d[n - 1][m - 1];
    }
}

//20 20
//        0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 0 0 0 0
//        0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 1 1
//        1 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 0
//        0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
//        0 1 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 1
//        1 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0
//        1 0 1 1 0 1 0 0 1 1 1 0 1 0 0 1 0 1 0 0
//        0 0 0 0 0 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0
//        0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 0 1 0
//        0 0 1 0 0 0 1 0 0 0 0 1 0 1 0 1 1 1 1 0
//        1 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1
//        1 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 1 0
//        0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 0
//        1 1 1 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0
//        0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0
//        1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 1 0 0 0
//        1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1
//        0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 1 0 1
//        0 1 0 1 1 1 1 0 1 0 0 0 1 0 0 1 0 0 0 1
//        1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0